package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.FileData;
import com.ruoyi.system.domain.FileDataLog;
import com.ruoyi.system.domain.FileDataVm;
import com.ruoyi.system.mapper.FileDataLogMapper;
import com.ruoyi.system.mapper.FileDataMapper;
import com.ruoyi.system.service.fileDataService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 用户 业务层处理
 * 
 * @author ruoyi
 */
@Service
public class fileDataImpl implements fileDataService
{
    private static final Logger log = LoggerFactory.getLogger(fileDataImpl.class);

    @Autowired
    private FileDataMapper fileDataMapper;
    @Autowired
    private FileDataLogMapper fileDataLogMapper;

    @Override
    public List<FileData> selectFileDataList(FileDataVm fileData) {
        List<String> list = new ArrayList<>();
        String menus = fileData.getMenus();
        if(StringUtils.isNoneBlank(menus)) {
            if(menus.indexOf(",")!=-1){
            String[] split = menus.split(",");

            for (int i = 0; i < split.length; i++) {
                String s = split[i];
                list.add(s);
            }
        }else{
                list.add(menus);
            }
            fileData.setListmenus(list);
        }

        return fileDataMapper.selectFileDataList(fileData);
    }

    @Override
    public int insertFileData(FileData fileData) {
        return fileDataMapper.insert(fileData);
    }

    @Override
    public int deleteUserByIds(Integer id,String loginName) {
        FileDataLog  fileDataLog=new FileDataLog();
        FileData fileData = fileDataMapper.selectByPrimaryKey(id);
        fileDataLog.setFileName(fileData.getFileName());
        fileDataLog.setDatatimes(new Date());
        fileDataLog.setStaff(loginName);
        fileDataLog.setLabel(fileData.getLabel());
        fileDataLog.setStatus("删除");
        int i = fileDataMapper.deleteByPrimaryKey(id);


        // 指定要删除的文件路径
        File file = new File(fileData.getFileUrl());

        // 检查文件是否存在
        if (file.exists()) {
            // 尝试删除文件
            if (file.delete()) {
                System.out.println("文件删除成功：" + file.getAbsolutePath());
            } else {
                System.out.println("文件删除失败：" + file.getAbsolutePath());
            }
        } else {
            System.out.println("文件不存在：" + file.getAbsolutePath());
        }


        fileDataLogMapper.insert(fileDataLog);
        return i;
    }

    @Override
    public List<FileDataLog> selectfilelogListList(FileDataLog fileDataLog) {
        return fileDataLogMapper.selectFileDataLogList(fileDataLog);
    }

    @Override
    public FileData getfileNameObj(String originalFilename) {
        return fileDataMapper.getfileNameObj(originalFilename);
    }
}
